package almacen.view;


import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import javax.swing.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
import javax.swing.text.MaskFormatter;
import almacen.bean.Proveedor;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * formProveedor.java
 *
 * Created on Mar 18, 2012, 1:32:40 PM
 */

/**
 *
 * @author pdrok
 */
public class formProveedor extends javax.swing.JFrame {
    SessionFactory fabrica = new Configuration().configure().buildSessionFactory();
    List<Proveedor> listado_proveedor = new ArrayList();
    int posicion_actual = 0;
    int tamano_listado = 0;
    String listar = "from Proveedor ";
    String orden = "order by pro_descripcion";
    MaskFormatter formatosector;
    /** Creates new form formProveedor */
    public formProveedor() {
        initComponents();
        rellenar_jtable();
        jTable1.setSelectionBackground(Color.yellow);
        bt_nuevo.requestFocus();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grupo_orden = new javax.swing.ButtonGroup();
        jLabel9 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane6 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        bt_nuevo = new javax.swing.JButton();
        bt_guardar = new javax.swing.JButton();
        bt_eliminar = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        bt_primero = new javax.swing.JButton();
        bt_anterior = new javax.swing.JButton();
        bt_siguiente = new javax.swing.JButton();
        bt_ultimo = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        tf_buscar = new javax.swing.JTextField();
        tf_descripcion = new javax.swing.JTextField();
        tf_codigo = new javax.swing.JTextField();
        tf_ruc = new javax.swing.JTextField();
        tf_telefono = new javax.swing.JTextField();
        tf_obs = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        orden_codigo = new javax.swing.JRadioButton();
        orden_nombre = new javax.swing.JRadioButton();
        jLabel10 = new javax.swing.JLabel();
        tf_email = new javax.swing.JFormattedTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        tf_direccion = new javax.swing.JTextField();
        tf_celular = new javax.swing.JTextField();

        jLabel9.setText("jLabel9");

        setTitle("Registro de Clientes");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setText("Registro de Proveedor");

        jLabel2.setText("Codigo. :");

        jLabel3.setText("Nombre.:");

        jLabel4.setText("RUC.:");

        jLabel5.setText("Telefono.:");

        jLabel6.setText("Observacion.:");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Codigo", "Nombre", "Ruc", "Telefono", "Celular", "Direccion", "Email", "Observacion"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane6.setViewportView(jTable1);

        bt_nuevo.setText("Nuevo");
        bt_nuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_nuevoActionPerformed(evt);
            }
        });
        bt_nuevo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                bt_nuevoKeyPressed(evt);
            }
        });

        bt_guardar.setText("Guardar");
        bt_guardar.setNextFocusableComponent(bt_nuevo);
        bt_guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_guardarActionPerformed(evt);
            }
        });
        bt_guardar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                bt_guardarKeyPressed(evt);
            }
        });

        bt_eliminar.setText("Eliminar");
        bt_eliminar.setNextFocusableComponent(bt_nuevo);
        bt_eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_eliminarActionPerformed(evt);
            }
        });

        jLabel7.setText("Celular");

        bt_primero.setText("|<");
        bt_primero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_primeroActionPerformed(evt);
            }
        });

        bt_anterior.setText("<");
        bt_anterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_anteriorActionPerformed(evt);
            }
        });

        bt_siguiente.setText(">");
        bt_siguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_siguienteActionPerformed(evt);
            }
        });

        bt_ultimo.setText(">|");
        bt_ultimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_ultimoActionPerformed(evt);
            }
        });

        jLabel8.setText("Buscar. :");

        tf_buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_buscarActionPerformed(evt);
            }
        });

        tf_descripcion.setNextFocusableComponent(tf_ruc);
        tf_descripcion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_descripcionActionPerformed(evt);
            }
        });
        tf_descripcion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tf_descripcionKeyPressed(evt);
            }
        });

        tf_codigo.setEditable(false);
        tf_codigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_codigoActionPerformed(evt);
            }
        });

        tf_ruc.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tf_rucKeyPressed(evt);
            }
        });

        tf_telefono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tf_telefonoKeyPressed(evt);
            }
        });

        tf_obs.setNextFocusableComponent(bt_guardar);
        tf_obs.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tf_obsKeyPressed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 255, 204));

        orden_codigo.setBackground(new java.awt.Color(255, 255, 204));
        grupo_orden.add(orden_codigo);
        orden_codigo.setText("Codigo");
        orden_codigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                orden_codigoActionPerformed(evt);
            }
        });

        orden_nombre.setBackground(new java.awt.Color(255, 255, 204));
        grupo_orden.add(orden_nombre);
        orden_nombre.setSelected(true);
        orden_nombre.setText("Nombre");
        orden_nombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                orden_nombreActionPerformed(evt);
            }
        });

        jLabel10.setText("Orden");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(orden_codigo)
                        .addGap(18, 18, 18)
                        .addComponent(orden_nombre))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(jLabel10)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(14, Short.MAX_VALUE)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(orden_codigo)
                    .addComponent(orden_nombre))
                .addContainerGap())
        );

        tf_email.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tf_emailKeyPressed(evt);
            }
        });

        jLabel11.setText("Email. :");

        jLabel12.setText("Direccion.:");

        tf_direccion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_direccionActionPerformed(evt);
            }
        });
        tf_direccion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tf_direccionKeyPressed(evt);
            }
        });

        tf_celular.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tf_celularKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addGap(18, 18, 18)
                                .addComponent(tf_email, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel6)
                                .addGap(18, 18, 18)
                                .addComponent(tf_obs, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(329, 329, 329))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel2)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(tf_codigo, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(tf_descripcion))
                            .addComponent(tf_buscar, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(tf_ruc, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
                                    .addComponent(tf_celular))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel5)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(tf_telefono, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(21, 21, 21)
                                        .addComponent(jLabel12)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(tf_direccion)))))
                        .addGap(18, 18, 18)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(bt_nuevo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_guardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_eliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_primero)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_anterior)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_siguiente)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_ultimo))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(172, 172, 172)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 768, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tf_buscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(tf_codigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2))
                                .addGap(9, 9, 9)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel4)
                                    .addComponent(tf_ruc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel5)
                                    .addComponent(tf_telefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(tf_descripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel3))
                                .addGap(29, 29, 29))))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel7)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel12)
                                .addComponent(tf_direccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(tf_celular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_obs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel11)
                    .addComponent(tf_email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_nuevo)
                    .addComponent(bt_guardar)
                    .addComponent(bt_eliminar)
                    .addComponent(bt_primero)
                    .addComponent(bt_anterior)
                    .addComponent(bt_siguiente)
                    .addComponent(bt_ultimo))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(30, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        int linea = jTable1.getSelectedRow();
        posicion_actual = linea;
       // JOptionPane.showMessageDialog(null, "linea = "+linea);
        /*
         tf_codigo.setText(jTable1.getValueAt(linea, 0).toString());
        tf_descripcion.setText(jTable1.getValueAt(linea, 1).toString());
        tf_ruc.setText(jTable1.getValueAt(linea, 2).toString());
        tf_telefono.setText(jTable1.getValueAt(linea, 3).toString());
        tf_sector.setText(jTable1.getValueAt(linea, 4).toString());
        tf_obs.setText(jTable1.getValueAt(linea, 5).toString());
        */
        Proveedor proveedor = new Proveedor();
        proveedor.setCodigo(Integer.parseInt(jTable1.getValueAt(linea,0).toString()));
        proveedor.setDescripcion(jTable1.getValueAt(linea,1).toString());
        proveedor.setRuc(jTable1.getValueAt(linea, 2).toString());
        proveedor.setTelefono(jTable1.getValueAt(linea, 3).toString());
        proveedor.setCelular(jTable1.getValueAt(linea, 4).toString());
        proveedor.setDireccion(jTable1.getValueAt(linea, 4).toString());
        proveedor.setEmail(jTable1.getValueAt(linea, 4).toString());
        proveedor.setObs(jTable1.getValueAt(linea, 5).toString());
        tf_codigo.setText(""+proveedor.getCodigo());
        tf_descripcion.setText(""+proveedor.getDescripcion());
        tf_ruc.setText(""+proveedor.getRuc());
        tf_telefono.setText(""+proveedor.getTelefono());
        tf_celular.setText(""+proveedor.getCelular());
        tf_email.setText(""+proveedor.getEmail());
        tf_obs.setText(""+proveedor.getObs());
        

        
    }//GEN-LAST:event_jTable1MouseClicked

    private void bt_nuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_nuevoActionPerformed
        tf_codigo.setText("");
        tf_descripcion.setText("");
        tf_ruc.setText("");
        tf_telefono.setText("");
        tf_celular.setText("");
        tf_direccion.setText("");
        tf_email.setText("");
        tf_obs.setText("");
        tf_descripcion.requestFocus();
        
    }//GEN-LAST:event_bt_nuevoActionPerformed

    private void bt_guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_guardarActionPerformed
         try
        {
            Session seccion = fabrica.openSession();
            Proveedor proveedor = new Proveedor();
             if(! tf_codigo.getText().equals(""))
             proveedor.setCodigo(Integer.parseInt(tf_codigo.getText()));
            proveedor.setDescripcion(tf_descripcion.getText());
            proveedor.setRuc(tf_ruc.getText());
            proveedor.setTelefono(tf_telefono.getText());
            proveedor.setCelular(tf_celular.getText());
            proveedor.setDireccion(tf_direccion.getText());
            proveedor.setEmail(tf_email.getText());
            proveedor.setObs(tf_obs.getText());
            Transaction tx_proveedor = seccion.beginTransaction();
            seccion.saveOrUpdate(proveedor);
             tx_proveedor.commit();
             seccion.clear();
             rellenar_jtable();
        }
        catch(Exception erro)
        {
            JOptionPane.showMessageDialog(null,"Error en la insecion : "+erro );
        }
    }//GEN-LAST:event_bt_guardarActionPerformed

    private void bt_eliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_eliminarActionPerformed
        String eliminar = "Seguro que desea eliminar el proveedor "+tf_descripcion.getText()+"?";
        int opcion_elegida = JOptionPane.showConfirmDialog(null, eliminar, "Eliminar",JOptionPane.YES_NO_OPTION);
        if (opcion_elegida == JOptionPane.YES_OPTION)
        {

            try
            {
                Session seccion = fabrica.openSession();
                Proveedor proveedor = new Proveedor();
                proveedor.setCodigo(Integer.parseInt(tf_codigo.getText()));
                Transaction tx_proveedor = seccion.beginTransaction();
                seccion.delete(proveedor);
                 tx_proveedor.commit();
                 seccion.clear();
                 JOptionPane.showMessageDialog(null, "Registro eliminado con exito");
                 rellenar_jtable();
            }
            catch(Exception erro)
            {
                JOptionPane.showMessageDialog(null,"Error en la exclusion : "+erro );
            }

        }else
                JOptionPane.showMessageDialog(null, "Registro no eliminado");

    }//GEN-LAST:event_bt_eliminarActionPerformed

    private void bt_siguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_siguienteActionPerformed
        if (posicion_actual < (tamano_listado-1))
        {   posicion_actual ++;
            Proveedor proveedor = listado_proveedor.get(posicion_actual);
            tf_codigo.setText(""+proveedor.getCodigo());
            tf_descripcion.setText(proveedor.getDescripcion());
            tf_ruc.setText(proveedor.getRuc());
            tf_telefono.setText(proveedor.getTelefono());
            tf_celular.setText(""+proveedor.getCelular());
            tf_direccion.setText(proveedor.getDireccion());
            tf_email.setText(proveedor.getEmail());
            tf_obs.setText(proveedor.getObs());
            jTable1.setRowSelectionInterval(posicion_actual, posicion_actual);
        }
    }//GEN-LAST:event_bt_siguienteActionPerformed

    private void bt_anteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_anteriorActionPerformed
         if (posicion_actual > 0)
        {   posicion_actual --;
            Proveedor proveedor = listado_proveedor.get(0);
            tf_codigo.setText(""+proveedor.getCodigo());
            tf_descripcion.setText(proveedor.getDescripcion());
            tf_ruc.setText(proveedor.getRuc());
            tf_telefono.setText(proveedor.getTelefono());
            tf_celular.setText(""+proveedor.getCelular());
            tf_direccion.setText(proveedor.getDireccion());
            tf_email.setText(proveedor.getEmail());
            tf_obs.setText(proveedor.getObs());
             jTable1.setRowSelectionInterval(posicion_actual, posicion_actual);
        }
    }//GEN-LAST:event_bt_anteriorActionPerformed

    private void bt_ultimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_ultimoActionPerformed


             Proveedor proveedor = listado_proveedor.get(0);
            tf_codigo.setText(""+proveedor.getCodigo());
            tf_descripcion.setText(proveedor.getDescripcion());
            tf_ruc.setText(proveedor.getRuc());
            tf_telefono.setText(proveedor.getTelefono());
            tf_celular.setText(""+proveedor.getCelular());
            tf_direccion.setText(proveedor.getDireccion());
            tf_email.setText(proveedor.getEmail());
            tf_obs.setText(proveedor.getObs());
            posicion_actual = tamano_listado - 1;
            jTable1.setRowSelectionInterval(posicion_actual, posicion_actual);
    }//GEN-LAST:event_bt_ultimoActionPerformed

    private void bt_primeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_primeroActionPerformed
            Proveedor proveedor = listado_proveedor.get(0);
            tf_codigo.setText(""+proveedor.getCodigo());
            tf_descripcion.setText(proveedor.getDescripcion());
            tf_ruc.setText(proveedor.getRuc());
            tf_telefono.setText(proveedor.getTelefono());
            tf_celular.setText(""+proveedor.getCelular());
            tf_direccion.setText(proveedor.getDireccion());
            tf_email.setText(proveedor.getEmail());
            tf_obs.setText(proveedor.getObs());
            posicion_actual = 0;
            jTable1.setRowSelectionInterval(posicion_actual, posicion_actual);
    }//GEN-LAST:event_bt_primeroActionPerformed

    private void tf_buscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_buscarActionPerformed
        listar = "from Proveedor where upper(pro_descripcion) like '%"+tf_buscar.getText().toUpperCase()+"%'";
            rellenar_jtable();
}//GEN-LAST:event_tf_buscarActionPerformed

    private void tf_descripcionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_descripcionActionPerformed
           

    }//GEN-LAST:event_tf_descripcionActionPerformed

    private void orden_nombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_orden_nombreActionPerformed
        orden = "order by pro_descripcion";
        rellenar_jtable();
    }//GEN-LAST:event_orden_nombreActionPerformed

    private void orden_codigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_orden_codigoActionPerformed
       orden ="order by pro_codigo";
       rellenar_jtable();
    }//GEN-LAST:event_orden_codigoActionPerformed

    private void tf_descripcionKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_descripcionKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER)
        {
            tf_ruc.requestFocus();
        }
    }//GEN-LAST:event_tf_descripcionKeyPressed

    private void bt_nuevoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_bt_nuevoKeyPressed
         if (evt.getKeyCode() == evt.VK_ENTER)
        {
           bt_nuevo.doClick();
           tf_descripcion.requestFocus();
        }
    }//GEN-LAST:event_bt_nuevoKeyPressed

    private void tf_rucKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_rucKeyPressed
         if (evt.getKeyCode() == evt.VK_ENTER)
        {
            tf_telefono.requestFocus();
        }
    }//GEN-LAST:event_tf_rucKeyPressed

    private void tf_telefonoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_telefonoKeyPressed
         if (evt.getKeyCode() == evt.VK_ENTER)
        {
            tf_celular.requestFocus();
        }
    }//GEN-LAST:event_tf_telefonoKeyPressed

    private void tf_obsKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_obsKeyPressed
         if (evt.getKeyCode() == evt.VK_ENTER)
        {
            bt_guardar.requestFocus();
        }
    }//GEN-LAST:event_tf_obsKeyPressed

    private void bt_guardarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_bt_guardarKeyPressed
           if (evt.getKeyCode() == evt.VK_ENTER)
        {
           bt_guardar.doClick();
           bt_nuevo.requestFocus();
        }
    }//GEN-LAST:event_bt_guardarKeyPressed

    private void tf_codigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_codigoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_codigoActionPerformed

    private void tf_direccionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_direccionActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_direccionActionPerformed

    private void tf_celularKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_celularKeyPressed
         if (evt.getKeyCode() == evt.VK_ENTER)
        {
            tf_direccion.requestFocus();
        }
    }//GEN-LAST:event_tf_celularKeyPressed

    private void tf_direccionKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_direccionKeyPressed
         if (evt.getKeyCode() == evt.VK_ENTER)
        {
            tf_email.requestFocus();
        }
    }//GEN-LAST:event_tf_direccionKeyPressed

    private void tf_emailKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_emailKeyPressed
         if (evt.getKeyCode() == evt.VK_ENTER)
        {
            tf_obs.requestFocus();
        }
    }//GEN-LAST:event_tf_emailKeyPressed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new formProveedor().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_anterior;
    private javax.swing.JButton bt_eliminar;
    private javax.swing.JButton bt_guardar;
    private javax.swing.JButton bt_nuevo;
    private javax.swing.JButton bt_primero;
    private javax.swing.JButton bt_siguiente;
    private javax.swing.JButton bt_ultimo;
    private javax.swing.ButtonGroup grupo_orden;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JTable jTable1;
    private javax.swing.JRadioButton orden_codigo;
    private javax.swing.JRadioButton orden_nombre;
    private javax.swing.JTextField tf_buscar;
    private javax.swing.JTextField tf_celular;
    private javax.swing.JTextField tf_codigo;
    private javax.swing.JTextField tf_descripcion;
    private javax.swing.JTextField tf_direccion;
    private javax.swing.JFormattedTextField tf_email;
    private javax.swing.JTextField tf_obs;
    private javax.swing.JTextField tf_ruc;
    private javax.swing.JTextField tf_telefono;
    // End of variables declaration//GEN-END:variables
public void rellenar_jtable()
{
    jTable1.getColumnModel().getColumn(0).setPreferredWidth(40);
    jTable1.getColumnModel().getColumn(1).setPreferredWidth(100);
    jTable1.getColumnModel().getColumn(2).setPreferredWidth(60);
    jTable1.getColumnModel().getColumn(3).setPreferredWidth(60);
    jTable1.getColumnModel().getColumn(4).setPreferredWidth(60);
    jTable1.getColumnModel().getColumn(5).setPreferredWidth(100);

    DefaultTableModel modelo = (DefaultTableModel)jTable1.getModel();
    modelo.setNumRows(0);
   try
   {
        Session seccion = fabrica.openSession();
        listado_proveedor = seccion.createQuery(listar+orden).list();
        tamano_listado = listado_proveedor.size();
        String datos="";
        for (int i=0; i < tamano_listado;i++)
          {
                    Proveedor proveedor = listado_proveedor.get(i);
               
                    modelo.addRow(new Object []{proveedor.getCodigo(),proveedor.getDescripcion(),
                                    proveedor.getRuc(),proveedor.getTelefono(),proveedor.getCelular(),
                                    proveedor.getDireccion(),proveedor.getEmail(),proveedor.getObs()});
          }
        if (! listado_proveedor.isEmpty())
        {
            posicion_actual = 0;
           Proveedor proveedor = listado_proveedor.get(0);
            tf_codigo.setText(""+proveedor.getCodigo());
            tf_descripcion.setText(proveedor.getDescripcion());
            tf_ruc.setText(proveedor.getRuc());
            tf_telefono.setText(proveedor.getTelefono());
            tf_celular.setText(""+proveedor.getCelular());
            tf_direccion.setText(proveedor.getDireccion());
            tf_email.setText(proveedor.getEmail());
            tf_obs.setText(proveedor.getObs());
            jTable1.setRowSelectionInterval(0, 0);
        }
   }
    catch (Exception error){
    JOptionPane.showMessageDialog(null, "Erro al listar en  JTable "+error);
    }



}
}


